/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_API_REFLECTION_TYPED_HPP
#define	_CUBEFS_API_REFLECTION_TYPED_HPP

#include <cubefs/api/macro.hpp>
#include <cubefs/api/types.hpp>

#include <cubefs/api/reflection/type_name.hpp>

#include <string>

namespace cubefs    {
namespace api       {

const Int DESTROY_PEER = 0;


struct Type;
struct Typed;

typedef void (*PeerNotifier)(Typed *target, Int signal);
typedef void (*PeerCleanup)(Typed *target);

struct CUBEFS_API Typed {

    virtual ~Typed() throw () {}

    virtual bool IsInstance(const Type *type) const 	= 0;
    virtual Type* type() const 							= 0;
    virtual bool managed() const 						= 0;
    virtual BigInt GetPeer() const  					= 0;

    virtual void SetPeer(BigInt peer) 					= 0;
    virtual Int GetPeerFlags() const					= 0;
    virtual void SetPeerFlags(Int flags) 				= 0;
    virtual const Int &peer_flags() const 				= 0;
    virtual Int &peer_flags() 							= 0;
    virtual void CleanupPeer()							= 0;
    virtual void notify(Int signal) 					= 0;

    static void SetPeerNotifier(PeerNotifier notifier);
    static void SetPeerCleanup(PeerCleanup cleanup);

    static Type* MyType();
};

CUBEFS_DECLARE_TYPENAME(Typed, "cubefs::api::Typed");

const Int UnknownType		= 0;
const Int NumberValueType	= 1;
const Int TypedValueType	= NumberValueType;
const Int TypedType			= 3;

template <typename T>
struct ListType {
	static const Int TypeValue = UnknownType;
};


#define CUBEFS_LIST_VALUE_TRAITS(TypeName, TypeCode)	\
template <>												\
struct ListType<TypeName> {								\
	static const Int TypeValue = TypeCode;				\
}


CUBEFS_LIST_VALUE_TRAITS(Byte, 		NumberValueType);
CUBEFS_LIST_VALUE_TRAITS(UByte, 	NumberValueType);
CUBEFS_LIST_VALUE_TRAITS(Short,	 	NumberValueType);
CUBEFS_LIST_VALUE_TRAITS(UShort, 	NumberValueType);
CUBEFS_LIST_VALUE_TRAITS(Int, 		NumberValueType);
CUBEFS_LIST_VALUE_TRAITS(UInt, 		NumberValueType);
CUBEFS_LIST_VALUE_TRAITS(BigInt, 	NumberValueType);
CUBEFS_LIST_VALUE_TRAITS(float, 	NumberValueType);
CUBEFS_LIST_VALUE_TRAITS(double, 	NumberValueType);

CUBEFS_LIST_VALUE_TRAITS(Typed*, 	TypedType);
CUBEFS_LIST_VALUE_TRAITS(Type*, 	TypedType);

}
}

#endif
